<?php
	$validation_rules = array(
		"onair_episode_no" => array(
			"required" => true,
            'positiveInt' => true,
            'epsExist' => true,
            'maxlength' => 9
		),
        'programme_title_cn' => array(
            'maxlength' => 255
        ),
        'programme_title_en' => array(
            'maxlength' => 255
        ),
        'programme_path' => array(
            'required' => true,
            #'path' => true,
            'maxlength' => 255
        ),
		"dish_name_chinese" => array(
			"maxlength" => "255",
		), 
		"dish_name_english" => array(
			"maxlength" => "255"
		),
        'publish_datetime[]' => array(
            'required' => TRUE
        ),
        'expire_datetime[]' => array(
            'required' => TRUE
        ),
	);
?>

<?php

    if ($act==='view') {
        FormHelper::setReadonly(true);
        $validation_rules = array();
    }

	$recipe = isset($data['recipe']) ? $data['recipe'] : "";
	$episode = isset($data['episode']) ? $data['episode'] : "";
	$programme = isset($data['programme']) ? $data['programme'] : "";
	$recipe_json_list = isset($data['recipe_json_list']) ? $data['recipe_json_list'] : array();
	$form_action = ($act === 'view') ? "info_switch.html?type=recipe" : ( ($act === 'add') ? "add_recipe.html?type=recipe&act=add" : "update_recipe.html?type=recipe&act=update");
	$publish = isset($data['unpublish']) ? $data['unpublish'] : 0;
	$from    = isset($data['from']) ? $data['from'] : '';
?>

<div id="recipe" class="mod ui-widget-content ui-helper-clearfix ui-corner-all <?php echo $act; ?>">

    <div class="hd ui-widget-header ui-helper-clearfix ui-corner-all"><h2><?php echo $act_text; ?> Recipe</h2></div>

    <div class="bd">
    <?php FormHelper::StartForm('ane', AC::PDES_BASE_URL.$form_action) ?>
	    <?php FormHelper::ErrorMessages($errors) ?>
		<?php if($act != 'add') FormHelper::HiddenField('id', $recipe->id) ?>
		<?php if($act != 'add') FormHelper::HiddenField('episode_id', $recipe->episode_id) ?>
		<?php FormHelper::HiddenField('check_used', $data['check_used']) ?>
		<?php FormHelper::HiddenField('status', $recipe->status) ?>
		<?php FormHelper::HiddenField('created_at', $recipe->created_at) ?>
		<?php FormHelper::HiddenField('created_by', $recipe->created_by) ?>
		<?php FormHelper::HiddenField('modified_at', $recipe->modified_at) ?>
		<?php FormHelper::HiddenField('modified_by', $recipe->modified_by) ?>
	    <?php FormHelper::HiddenField('from', $from) ?>
    	<fieldset class="ui-widget-content">
            <legend class="ui-widget-header">Find Programme Record</legend>
<?php
	if ($act != 'add')
	{
?>
			<?php FormHelper::ProtectedField($programme->id, 'Programme ID');?>
            <?php FormHelper::ProtectedField($recipe->id, 'Recipe ID');?>
            <?php 
				FormHelper::ProtectedField($programme->zh_programme_name, 'Programme Title (Chinese)');
				FormHelper::ProtectedField($programme->en_programme_name, 'Programme Title (English)');
				FormHelper::ProtectedField($programme->path, 'Programme Path') ?>
			<?php FormHelper::ProtectedField($episode->onair_episode_no, 'On Air Episode No.') ?>
			<?php FormHelper::HiddenField('programme_id', $programme->id) ?>
			<?php FormHelper::HiddenField('onair_episode_no', $episode->onair_episode_no) ?>
<?php
	}
	else
	{
?>
            <input id="programme_id" name="programme_id" type="hidden" value="<?php echo $recipe->programme_id;?>" />
			<input id="programme_eps" type="hidden" value="<?php echo $recipe->onair_episode_no;?>" />

            <div class="form-item">
                <span class="grid-l"><label><ins>**</ins>Programme Title (Chinese)</label> :</span>
                <span class="grid-r">
                <input id="programme_title_cn" name="programme_title_cn" type="text" class="text" autocomplete="off" 
                    value="<?php echo htmlentities($recipe->programme_title_cn, ENT_QUOTES, 'UTF-8');?>" />
                </span>
            </div>
            <div class="form-item">
                <span class="grid-l"><label><ins>**</ins>Programme Title (English)</label> :</span>
                <span class="grid-r">
                <input id="programme_title_en" name="programme_title_en" type="text" class="text" autocomplete="off" 
                    value="<?php echo htmlentities($recipe->programme_title_en, ENT_QUOTES, 'UTF-8');?>" /> 
                </span>
            </div>

            <div class="form-item">
                <span class="grid-l"><label><ins>*</ins>Programme Path</label> :</span>
                <span class="grid-r">
                <input id="programme_path" name="programme_path" type="text" class="text" autocomplete="off" 
                    value="<?php echo htmlentities($recipe->programme_path, ENT_QUOTES, 'UTF-8');?>" /> 
                </span>
            </div>

            <?php FormHelper::TextField('onair_episode_no', $recipe->onair_episode_no, "On Air Episode No.", '', 'number', 1) ?>
<?php
	}
?>
	</fieldset>
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Recipe Information</legend>
			<?php FormHelper::TextField('dish_name_chinese', $recipe->dish_name_chinese, 'Dish Name方劑名稱(Chinese)', false, 'ruler input_status', 2) ?>
			<?php FormHelper::TextField('dish_name_english', $recipe->dish_name_english, 'Dish Name方劑名稱(English)', false, 'ruler input_status', 2) ?>
			<div id="cuisine-category-selector">
<?php
	if ($act != 'view')
	{
		$cuisine_category_arr = PDES_GD::$cuisine_category;
	}
	else
	{
		PDES_GD::$cuisine_category[0] = '';
		$cuisine_category_arr = PDES_GD::$cuisine_category;
	}
?>
        	<?php FormHelper::DropDownList('cuisine_category', $cuisine_category_arr, $recipe->cuisine_category, 'Cuisine Category 菜系'); ?>
<?php
	if(empty($recipe->cuisine_category)){
		PDES_GD::$cuisine_sub_category[0] = '';
		$cuisine_sub_data = array(PDES_GD::$cuisine_sub_category[0]);
	}else{
		$cuisine_sub_data = PDES_GD::$cuisine_sub_category[(int)$recipe->cuisine_category];
	}
?>
        	<?php FormHelper::DropDownList('cuisine_sub_category', $cuisine_sub_data, $recipe->cuisine_sub_category, 'Cuisine Sub Category 菜系 '); ?>
			</div>
<?php
	if ($act != 'view')
	{
		$food_category_arr = PDES_GD::$food_category;
	}
	else
	{
		PDES_GD::$food_category[0] = '';
		$food_category_arr = PDES_GD::$food_category;
	}
?>
            <?php FormHelper::DropDownList('food_category', $food_category_arr, $recipe->food_category, 'Food Category 菜式') ?>
<?php
	if ($act != 'view')
	{
		$food_method_arr = PDES_GD::$food_method_type;
	}
	else
	{
		PDES_GD::$food_method_type[0] = '';
		$food_method_arr = PDES_GD::$food_method_type;
	}
?>
			<?php FormHelper::DropDownList('method_type', $food_method_arr, $recipe->method_type, 'Method Type') ?>
<?php
	if ($act != 'view')
	{
		$food_ingredients_arr = PDES_GD::$food_ingredients;
	}
	else
	{
		PDES_GD::$food_ingredients[0] = '';
		$food_ingredients_arr = PDES_GD::$food_ingredients;
	}
?>
			<?php FormHelper::DropDownList('main_ingredients', $food_ingredients_arr, $recipe->main_ingredients, 'Main Ingredients 主要材料') ?>
			<?php 
				FormHelper::TextArea('ingredients', $recipe->ingredients, 'Ingredients 材料');
				FormHelper::TextArea('seasonings', $recipe->seasonings, 'Seasonings 醃料');
				FormHelper::TextArea('sauce', $recipe->sauce, 'Sauce 汁料');
				FormHelper::TextArea('cooking_method', $recipe->cooking_method, 'Cooking Method 煮法');
				FormHelper::TextArea('hints', $recipe->hints, 'Hints 竅門');
									 
                $arr_cast_type = array(
                    'designed_by' => 'Designed by 食譜設計',
                    'presented_by' => 'Presented by'
                );
				FormHelper::GlobalNameInput($arr_cast_type, $recipe_json_list, 1);
				
				FormHelper::TextArea('purpose', $recipe->purpose, 'Purpose 主治功效');
				FormHelper::TextArea('useful_information', $recipe->useful_information, 'Useful Information方解');
				FormHelper::TextArea('description', $recipe->description, 'Description');
			?>
            <?php 
            if(in_array($data['_gn'],array('admin','proofreader','editor'))){
                if (!is_array(@$recipe->publish_datetime)) {
                    $recipe->publish_datetime = CommonUtils::formatDate(@$recipe->publish_datetime);
                }
				if (!is_array(@$recipe->expire_datetime)) {
                    $recipe->expire_datetime = CommonUtils::formatDate(@$recipe->expire_datetime);
				}
			    FormHelper::DateTimePicker('publish_datetime', $recipe->publish_datetime, 'Publish Date/Time (YYYY-MM-DD / HH:MM)', @$recipe_diff['publish_datetime'], 1);
			    FormHelper::DateTimePicker('expire_datetime', $recipe->expire_datetime, 'Expiry Date/Time (YYYY-MM-DD / HH:MM)', @$recipe_diff['expire_datetime'], 1);
            }
            ?>
		</fieldset>
<?php
	if ($act != "add")
	{
?>
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Record Status and Access Log</legend>
        	    <?php FormHelper::ProtectedField(AC::$op_status[$recipe->status], 'Record Status', true) ?>
        	    <?php FormHelper::ProtectedField($recipe->created_by, 'Created By') ?>
        	    <?php FormHelper::ProtectedField($recipe->created_at, 'Created At') ?>
        	    <?php FormHelper::ProtectedField($recipe->modified_by, 'Modified By') ?>
        	    <?php FormHelper::ProtectedField($recipe->modified_at, 'Modified At') ?>
		</fieldset>
<?php
	}
?>
        <?php FormHelper::DisplayPublishLog($data['publishLog']); ?>
        <span class="buttons">
            <?php
                switch ($act) {
                    case 'add':
                        FormHelper::SubmitButton('create', 'Create'); 
                        FormHelper::ResetButton('reset', 'Reset');
                    break;
                    case 'edit':
                        FormHelper::SubmitButton('update', 'Update');
                        FormHelper::ResetButton('reset', 'Reset');
                    	FormHelper::Button('back', 'Back to list');
                    break;
					case 'view':
						/** modified by vanni for bug no:260 **/
						if($publish ==1){
                            CommonUtils::showUnpublishButton($data, $data['_g']);
                        }else{
							if($recipe->status == AC::STATUS_DELETED){
								FormHelper::SubmitButton('recover', 'Recover');
							}else{
								FormHelper::SubmitButton('edit', 'Edit');
                                CommonUtils::showDeleteButton($data['publishLog']);
							}
						}
                    	FormHelper::Button('back', 'Back to list');
                    break;
                }
            ?>
        </span>
    <?php FormHelper::EndForm() ?>

    </div>

</div>

<script>
    TVB.Pdes.oValidation = {
        form: document.forms['ane'],
        rules: <?php echo json_encode($validation_rules); ?>
    };
</script>
